package gk.example.adapter;

import gk.example.activity.OtherInfoActivity;
import gk.example.bean.DateInfo;
import gk.example.bean.User;
import gk.example.util.ImageLoadUtil;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.gkdate.R;

/**
 * 
 * Description:约会列表的适配器
 * 
 * @author Wenk.Chan（最新修改者）
 * @version 1.0（最新版本号）
 */
public class DateListAdapter extends BaseAdapter {
	private Context mContext;
	private List<DateInfo> mListData;
	private List<User> mListUser;

	public DateListAdapter(Context context, List<DateInfo> listData, List<User> listUser) {
		mContext = context;
		mListData = listData;
		mListUser=listUser;

	}

	@Override
	public int getCount() {
		return mListData.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private static final int DATESTATUS_DONE = 0;
	private static final int DATESTATUS_GOING = 1;
	private static final int DATESTATUS_READY = 2;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		DataListViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new DataListViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_date_list, parent, false);
			viewHolder.imageSex = (ImageView) convertView
					.findViewById(R.id.iv_attend_sex);
			viewHolder.imageUserHead = (ImageView) convertView
					.findViewById(R.id.iv_user_head);
			viewHolder.tvUserName = (TextView) convertView
					.findViewById(R.id.tv_user_name);
			viewHolder.tvDateDetail = (TextView) convertView
					.findViewById(R.id.tv_date_detail);
			viewHolder.tvAnnounceTime = (TextView) convertView
					.findViewById(R.id.tv_announce_time);
			viewHolder.tvDatePeople = (TextView) convertView
					.findViewById(R.id.tv_date_people);
			viewHolder.tvDateStatus = (TextView) convertView
					.findViewById(R.id.tv_date_status);
			viewHolder.tvDateDate = (TextView) convertView
					.findViewById(R.id.tv_date_date);
			viewHolder.tvDateTime = (TextView) convertView
					.findViewById(R.id.tv_date_time);
			viewHolder.tvDatePlace = (TextView) convertView
					.findViewById(R.id.tv_date_place);
			viewHolder.tvDateContact = (TextView) convertView
					.findViewById(R.id.tv_date_contact);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (DataListViewHolder) convertView.getTag();
		}

		if (mListUser.get(position).getUserHead() != null) {
			ImageLoadUtil.getInstance().setImageBackground(
					mContext,
					false,
					viewHolder.imageUserHead,
					mListUser.get(position).getUserHead()
							.getFileUrl(mContext), null, 38, 38);
		}
		if (mListData.get(position).getDateDate() != null) {
			viewHolder.tvDateDate
					.setText(mListData.get(position).getDateDate());
		}
		if (mListUser.get(position).getSex() != null) {
			if (mListUser.get(position).getSex().equals("男")) {
				viewHolder.imageSex.setImageResource(R.drawable.ic_sex_male);
			} else {
				viewHolder.imageSex.setImageResource(R.drawable.ic_sex_female);
			}
		}
		if (mListData.get(position).getDateTime() != null) {
			viewHolder.tvDateTime
					.setText(mListData.get(position).getDateTime());
		}
		if (mListData.get(position).getDatePlace() != null) {
			viewHolder.tvDatePlace.setText(mListData.get(position)
					.getDatePlace());
		}

		if (mListUser.get(position).getContact() != null) {
			viewHolder.tvDateContact.setText(mListUser.get(position)
					.getContact());
		}
		if (mListUser.get(position).getNickName() != null) {
			viewHolder.tvUserName.setText(mListUser.get(position)
					.getNickName());
		}
		if (mListData.get(position).getDateDetail() != null) {
			if (mListData.get(position).getDateDetail().getBytes().length > 30) {
				String temp = mListData.get(position).getDateDetail()
						.substring(0, 10);
				viewHolder.tvDateDetail.setText(temp + "...");
			} else {
				viewHolder.tvDateDetail.setText(mListData.get(position)
						.getDateDetail());
			}
		}
		if (mListData.get(position).getCreatedAt() != null) {
			viewHolder.tvAnnounceTime.setText(mListData.get(position)
					.getCreatedAt());
		}
		if (mListData.get(position).getDateCurrentPeople() != null
				&& mListData.get(position).getDatePeople() != null) {
			viewHolder.tvDatePeople.setText("人数:"
					+ mListData.get(position).getDateCurrentPeople() + "/"
					+ mListData.get(position).getDatePeople());
		}

		int tempStr = mListData.get(position).getDateStatus();
		if (tempStr == DATESTATUS_DONE) {
			viewHolder.tvDateStatus.setText("已结束");
			viewHolder.tvDateStatus.setTextColor(mContext.getResources()
					.getColor(R.color.date_color_done));
		} else if (tempStr == DATESTATUS_GOING) {
			viewHolder.tvDateStatus.setText("进行中");
			viewHolder.tvDateStatus.setTextColor(mContext.getResources()
					.getColor(R.color.date_color_going));
		} else if (tempStr == DATESTATUS_READY) {
			viewHolder.tvDateStatus.setText("准备中");
			viewHolder.tvDateStatus.setTextColor(mContext.getResources()
					.getColor(R.color.date_color_ready));
		}
		viewHolder.imageUserHead.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, OtherInfoActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("userid", mListData.get(position)
						.getAnnouncer().getObjectId());
				intent.putExtras(bundle);
				mContext.startActivity(intent);
			}
		});

		return convertView;
	}
}

class DataListViewHolder {
	ImageView imageUserHead;
	ImageView imageSex;
	TextView tvUserName;
	TextView tvAnnounceTime;
	TextView tvDateTime;
	TextView tvDateDate;
	TextView tvDatePlace;
	TextView tvDateDetail;
	TextView tvDatePeople;
	TextView tvDateStatus;
	TextView tvDateContact;
}
